package com.md.youyouhui.ui.login;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityForgetPassOneBinding;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by AdministratorZJ
 * on 2019/4/3.
 *
 * @author AdministratorZJ
 */
public class ForgetOneActivity extends BaseActivity {
    private ActivityForgetPassOneBinding oneBinding;
    private Timer timer = new Timer();
    private TimerTask timerTask;
    private int time = 60;
    private MainViewModel viewModel;

    @Override
    public void setContentLayout() {
        oneBinding = DataBindingUtil.setContentView(this, R.layout.activity_forget_pass_one);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(oneBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        oneBinding.registerBtn.setOnClickListener(v -> {
            forgotNext();
        });
        oneBinding.timeCode.setOnClickListener(v -> sendCodeService());
    }

    private void sendCodeService() {
        String phone = oneBinding.forgetPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        viewModel.sendCode(phone).observe(this, codeResponse -> {
            assert codeResponse != null;
            switch (codeResponse.status) {
                case LOADING:
                    break;
                case SUCCESS:
                    sendCode();
                    break;
                case ERROR:
                    ToastUtils.errMake(this, codeResponse.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    private void forgotNext() {
        String phone = oneBinding.forgetPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        String code = oneBinding.forgetCode.getText().toString();
        if (TextUtils.isEmpty(code)) {
            Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        startActivityForResult(new Intent(ForgetOneActivity.this, ForgetTwoActivity.class).putExtra("phone", phone)
                        .putExtra("code", code)
                , 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            finish();
        }
    }

    private void sendCode() {
        timerTask = new TimerTask() {
            @Override
            public void run() {
                time--;
                runOnUiThread(() -> {
                    oneBinding.timeCode.setText(String.format("%dS", time));
                    if (time == 0) {
                        time = 60;
                        oneBinding.timeCode.setText("发送验证码");
                        oneBinding.timeCode.setEnabled(true);
                        timerTask.cancel();
                    } else {
                        oneBinding.timeCode.setEnabled(false);
                    }
                });
            }
        };
        timer.schedule(timerTask, 100, 1000);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null == timer) {
            return;
        }
        if (null != timerTask) {
            timerTask.cancel();
        }
        timer.purge();
        timer.cancel();
    }
}
